package com.kadron.account;

import java.io.Serializable;

import javax.transaction.UserTransaction;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.transaction.Transaction;

import com.kadron.common.KConst;
import com.kadron.common.KUtil;
import com.kadron.common.PasswordUtil;
import com.kadron.dao.KUserDAO;
import com.kadron.dao.KUserImageDAO;
import com.kadron.dao.KUserKadronsDAO;
import com.kadron.entity.User;
import com.kadron.entity.UserImage;
import com.kadron.exceptions.KPersistenceException;
import com.kadron.manager.KMessageManager;
import com.kadron.session.KUser;
import com.kadron.transaction.KTx;

@AutoCreate
@Name("kMyAccount")
@Scope(ScopeType.PAGE)
public class KMyAccount extends KMessageManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5307825474425830753L;
	
	private AccountData accountData;
	private UserImage img;
	private User user;
	private long totalKadrons;
	
	@In
	private KUserDAO userDAO;
	
	@In
	private KUserKadronsDAO userKadronsDAO;
	
	@In
	private KUserImageDAO userImageDAO;
	
	@Create
	public void init() {
		loadData();
	}
	
	private void loadData() {		
		user = userDAO.find(KUser.instance().getUser().getId(), User.class);
		if (user == null) {
			setError(true);			
			return;
		}
		Long userKadrons = userKadronsDAO.getUserKadrons(user.getId());
		if (userKadrons != null) {
			totalKadrons = userKadrons;
		}
		img = userImageDAO.findByUser(user.getId());
		KUser.instance(user);
		accountData = new AccountData(user);
	}
	
	public AccountData getAccountData() {
		return accountData;
	}

	public void save() {
		if (KUtil.isEmpty(accountData.getFirstname())
				|| KUtil.isEmpty(accountData.getLastname())
				|| KUtil.isEmpty(accountData.getNickname())
				|| KUtil.isEmpty(accountData.getTel())
				|| KUtil.isEmpty(accountData.getMob())
				|| accountData.getBirthday() == null
				|| KUtil.isEmpty(accountData.getEmail())) {
			bad("field_values_are_not_correct");
		}
//		KUserDAO kUserDAO = (KUserDAO) Component.getInstance("kUserDAO");
//		boolean exists = kUserDAO.check(accountData.getUsername(), accountData.getEmail());
//		if (exists) {
//			message("user_exist_msg", StatusMessage.Severity.WARN);
//			return;
//		}
		user.updateData(accountData);
		UserTransaction transaction = null;
		try {
			transaction = Transaction.instance();
			user = (User) userDAO.update(user);	
			transaction.commit();
			good();
		} catch (KPersistenceException e) {
			KTx.rollback(transaction);
			KTx.begin(transaction);
			bad(KConst.VERSION_CHANGED);
		} catch (Exception e) {
			e.printStackTrace();
			bad(KConst.BAD_THING);
			KTx.rollback(transaction);
			KTx.begin(transaction);
		}
		loadData();
	}
	
	public void savePassword() {
		if (KUtil.isEmpty(accountData.getPassword()) 
				|| KUtil.isEmpty(accountData.getConfirmPassword()) 
				|| !accountData.getPassword().equals(accountData.getConfirmPassword())) {
			bad("field_values_are_not_correct");
			return;
		}
		String password = accountData.getPassword();
		String criptedPassword = PasswordUtil.cript(password);
		user.setPassword(criptedPassword);
		UserTransaction tx = null;
		try {
			tx = Transaction.instance();
			user = (User) userDAO.update(user);	
			tx.commit();
			good();
		} catch (KPersistenceException e) {
			KTx.rollback(tx);
			KTx.begin(tx);
			bad(KConst.VERSION_CHANGED);
		} catch (Exception e) {
			e.printStackTrace();
			bad(KConst.BAD_THING);
			KTx.rollback(tx);
			KTx.begin(tx);
		}
		loadData();
	}
	
	public UserImage getImg() {
		return img;
	}

	public long getTotalKadrons() {
		return totalKadrons;
	}
	
}